package com.gopay.scheduler.service.sms.impl;

import com.gopay.common.constants.sms.SmsMassEnum;
import com.gopay.common.cps.service.sms.SmsMassBaseService;
import com.gopay.common.domain.sms.SmsMassContent;
import com.gopay.common.order.dao.CommonQueryDAO;
import com.gopay.scheduler.common.CronSmsMassConstants;
import com.gopay.scheduler.manager.sms.SmsMassManager;
import com.gopay.scheduler.service.sms.SmsMassCompensateService;
import com.gopay.scheduler.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
/*
 * Copyright (c) 2016. www.gopay.com.cn Inc.All rights reserved.
 * 文件名称 : SmsMassCompensateServiceImpl
 * 创建日期 : 17/7/7 17:04
 * Author  : baiyunpeng
 * 短信群发补偿机制
 */
@Service
public class SmsMassCompensateServiceImpl implements SmsMassCompensateService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private SmsMassBaseService smsMassBaseService;
    @Autowired
    private CommonQueryDAO commonQueryDAO;
    @Autowired
    private SmsMassManager smsMassManager;
    private @Value("#{webProperties['smsMassJobLoop']}") Integer smsLoop;

    @Override
    public void compensateSmsMass() {
        try {
            String mm = DateUtils.format(new Date(), "mm");
            int currentMinutes = Integer.parseInt(mm);
            if(currentMinutes % smsLoop ==0){
                logger.info(String.format("短信群发补偿定时任务发送日期为{%s},避让时间点{%s},短信补偿任务避让短信群发任务", DateUtils.format(new Date(),"yyyyMMdd"),mm));
                return;
            }
            if(CronSmsMassConstants.SMS_MASS_COMPENSATE_START){
                List<SmsMassContent> smsCompensateList = smsMassBaseService.selectTheSmsCompensate();
                if(smsCompensateList==null || smsCompensateList.size()==0){
                    CronSmsMassConstants.SMS_MASS_COMPENSATE_START = false;
                    logger.info(String.format("短信群发补偿定时任务发送日期为{%s},今日无数据,任务不执行", DateUtils.format(new Date(),"yyyyMMdd")));
                    return;
                }
                long startSw = System.currentTimeMillis();
                logger.info("短信群发补偿定时任务-------execute");
                for (SmsMassContent compensate : smsCompensateList) {
                    if (smsMassManager.judgeTime2ExecuteUpd(compensate, SmsMassEnum.SEND_OVER.getValue(), 1)){
                        logger.info(String.format("短信群发补偿定时任务发送日期为{%s},因补偿时间超时{超时时间为1小时},将状态contentId{%s}更新为成功", DateUtils.format(new Date(),"yyyyMMdd"),compensate.getSmsId()));
                        continue;
                    }
                    selectWillBeSendSmsByPage(compensate.getSmsId(), compensate.getContent());
                }
                long stopSw = System.currentTimeMillis();
                logger.info("短信群发补偿共耗费秒[{}]",(( stopSw*1.0 - startSw) / 1000));
            }
        } catch (Exception e) {
            logger.error("短信群发补偿定时任务异常",e);
        }
    }

    private void selectWillBeSendSmsByPage(String smsId,String content){
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT SMS_MASS_ID , PHONE_NUM FROM SMS_MASS_DETAIL WHERE SMS_CONTENT_ID='"+smsId+"' AND SEND_STATUS in ('0','3') ORDER BY CREATE_TS DESC ");
        smsMassManager.packageList(commonQueryDAO.findBySql(sql.toString()),smsId,content,CronSmsMassConstants.SMS_COMPENSATE);
    }
}
